﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using Common.Utilities;
using Common.Utilities.Dispatcher;
using Workflows.Components.TextMining.Properties;

namespace Workflows.Components.TextMining.TextScraping
{
    /// <summary>
    /// 
    /// </summary>
    [Serializable]
    public class PdfDocFullTextComponent:WorkflowComponentBase
    {
        #region props

        private PdfDocFullTextInstruction _Instruction;
        /// <summary>an object that encapsulate instruction settings.</summary>
        public override InstructionBase Instruction
        {
            get { return this._Instruction; }
            set { this._Instruction =(PdfDocFullTextInstruction) value;}
        }

        private PdfDocFullTextOutput _Output;
        /// <summary>an object to encapsulate output</summary>
        public override ResultBase Output
        {
            get { return this._Output; }
            set { this._Output = (PdfDocFullTextOutput) value; }
        }

        /// <summary></summary>
        public string SrcConnStr
        {
            get
            {
                return this._Instruction.SrcConnStr;
            }
            set
            {
                this._Instruction.SrcConnStr = value;
            }
        }

        /// <summary></summary>
        public string SelectSql
        {
            get
            {
                return this._Instruction.SelectSql;
            }
            set
            {
                this._Instruction.SelectSql = value;
            }
        }

        /// <summary></summary>
        public string FilePathField
        {
            get
            {
                return this._Instruction.FilePathField;
            }
            set
            {
                this._Instruction.FilePathField = value;
            }
        }

        /// <summary></summary>
        public string SrcIDField
        {
            get
            {
                return this._Instruction.SrcIDField;
            }
            set
            {
                this._Instruction.SrcIDField = value;
            }
        }

        /// <summary></summary>
        public string TgtConnStr
        {
            get
            {
                return this._Instruction.TgtConnStr;
            }
            set
            {
                this._Instruction.TgtConnStr = value;
            }
        }

        /// <summary></summary>
        public string TgtTableName
        {
            get
            {
                return this._Instruction.TgtTableName;
            }
            set
            {
                this._Instruction.TgtTableName = value;
            }
        }

        /// <summary></summary>
        public string TgtIDField
        {
            get
            {
                return this._Instruction.TgtIDField;
            }
            set
            {
                this._Instruction.TgtIDField = value;
            }
        }

        /// <summary></summary>
        public string FullTextField
        {
            get
            {
                return this._Instruction.FullTextField;
            }
            set
            {
                this._Instruction.FullTextField = value;
            }
        }

        /// <summary></summary>
        public string FlagField
        {
            get
            {
                return this._Instruction.FlagField;
            }
            set
            {
                this._Instruction.FlagField = value;
            }
        }

        /// <summary></summary>
        public string UpdField
        {
            get
            {
                return this._Instruction.UpdField;
            }
            set
            {
                this._Instruction.UpdField = value;
            }
        }
        #endregion

        #region ctor
        /// <summary>
        /// 
        /// </summary>
        public PdfDocFullTextComponent():base()
        {
            this.ComponentActivityType = WorkflowActivityType.SimpleActivity;
            this.ComponentCategory = WorkflowComponentCategory.TextMining.ToString();
            this.ComponentIcon = Resources.adobe;
            this.ComponentImage = Resources.UserMonitor;
            this.ComponentName = "PDF full text extractor";
            this.Description = "Extract pdf content as text";

            this._Instruction = new PdfDocFullTextInstruction(
                this.ComponentID, Guid.Empty,
                string.Empty, string.Empty, string.Empty, string.Empty,
                string.Empty, string.Empty, string.Empty, string.Empty,
                string.Empty, string.Empty);
            this._Output = new PdfDocFullTextOutput(
                this.ComponentID, Guid.Empty, 0, 0);
        }
        #endregion

        #region Overrides of WorkflowComponentBase


        /// <summary><para>must implementable method to serialize activity settings to xml</para></summary>
        public override XmlNode Serialize(ref XmlDocument xDoc, ref XmlNode parentNode)
        {
            XmlNode xNode = base.Serialize(ref xDoc, ref parentNode);

            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SrcConnStr", this.SrcConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SelectSql", this.SelectSql);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FilePathField", this.FilePathField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "SrcIDField", this.SrcIDField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtConnStr", this.TgtConnStr);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtTable", this.TgtTableName);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "TgtIDField", this.TgtIDField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FullTextField", this.FullTextField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "FlagField", this.FlagField);
            XmlDataUtil.UpdateAttribute(ref xDoc, xNode, "UpdField", "");

            return xNode;
        }

        /// <summary><para>subclass must implement this method to instantiate activity from xml</para></summary>
        public override WorkflowComponentBase Instantiate(XmlNode xNode)
        {
            if (xNode.Attributes.GetNamedItem("Type").Value == this.GetType().FullName)
            {
                PdfDocFullTextComponent wfComponent = base.Instantiate(xNode) as PdfDocFullTextComponent;

                wfComponent.SrcConnStr = XmlDataUtil.GetAttributeValue(xNode, "SrcConnStr", "");
                wfComponent.SelectSql = XmlDataUtil.GetAttributeValue(xNode, "SelectSql", "");
                wfComponent.FilePathField = XmlDataUtil.GetAttributeValue(xNode, "FilePathField", "");
                wfComponent.SrcIDField = XmlDataUtil.GetAttributeValue(xNode, "SrcIDField", "");
                wfComponent.TgtConnStr = XmlDataUtil.GetAttributeValue(xNode, "TgtConnStr", "");
                wfComponent.TgtTableName = XmlDataUtil.GetAttributeValue(xNode, "TgtTable", "");
                wfComponent.TgtIDField = XmlDataUtil.GetAttributeValue(xNode, "TgtIDField", "");
                wfComponent.FullTextField = XmlDataUtil.GetAttributeValue(xNode, "FullTextField", "");
                wfComponent.FlagField = XmlDataUtil.GetAttributeValue(xNode, "FlagField", "");
                wfComponent.UpdField = XmlDataUtil.GetAttributeValue(xNode, "UpdField", "");

                return wfComponent;
            }
            else
            {
                throw new Exception("Unable to instantiate component: invalid type specified in xml node");
            }
        }

        #endregion
    }
}
